home *** CD-ROM | disk | FTP | other *** search
-
-
-
-
- ELEMENTARY COMPUTER PROGRAMMING
- Lesson 1
-
- Copyright 1990
- Castle Oaks Computer Services
- Post Office Box 36082
- Indianapolis, IN 46236-0082
-
- COMPUTER ORGANIZATION
-
- Each model of computer usually differs from every other model in
- many of its details. However, all computers have the same general
- organization. This organization is shown in Figure I-1.
-
- +---------+
- +<<<<<<| CONTROL |>>>>>>+
- v +---------+ v
- v v ^ v v
- v v | v v
- +-------+ +--------+v +--------+
- | INPUT |->| MEMORY |-->| OUTPUT |
- +-------+ +--------+v +--------+
- | ^ v
- v | v
- +-----------------+
- | ARITHMETIC UNIT |
- +-----------------+
-
- Figure I-1
-
- There are five major elements. They are:
-
- a. MEMORY - This may consist of several types of memories
- which may be programmed to transfer data and/or instruc-
- tions from one type (or part) of memory to another as
- required. The single headed arrows in the figure indicate
- the normal flow of information between the memory and the
- other elements, and the lines of multiple arrows indicate
- the flow of control signals from the control unit to the
- other units.
-
- b. CONTROL - This unit calls instructions from the memory,
- decodes them, and controls all other elements in order to
- perform the required actions. Note: The difference be-
- tween a computer and a calculator is that a computer is a
- calculator that can store its instructions in its own
- memory.
-
- c. ARITHMETIC UNIT - This unit performs the necessary
- arithmetical and data manipulation operations.
-
- d. INPUT(s) - This unit may be a console keyboard, floppy
- disk, hard disk, etc. There may be several of these.
-
-
-
- I-1
-
-
- e. OUTPUT(s) - This unit may be a console screen, floppy
- disk, hard disk, printer, etc. There may be several of
- these. In most computers, the flow of data from input
- devices and to output devices is usually via the memory as
- shown in Figure I-1.
-
- A programmer working in a high level language, such as PASCAL,
- FORTRAN, BASIC, etc., need not know many of the details of the
- computer. However, the basic organization, as outlined above, is
- useful in organizing a problem for computer solution.
-
- In order to learn more about how computers operate, it is best to
- limit the discussion by presenting the details of only one model of
- computer. Once a person is familiar with the specifics of one
- particular model of computer, it is relatively easy to understand
- other models of computers, even more complex ones. Therefore, in
- this course, we will discuss a particular computer that is not very
- complex. The computer is called TRAC which is an acronym for TRAin-
- ing Computer. This computer does not exist in reality. However, we
- will use a simulation of this computer which runs on an IBM PC
- compatible computer. Using that simulator, we will execute TRAC
- programs as if they were running on a real TRAC. In the next les-
- son, we will cover the material you need to know to execute a TRAC
- program. In this lesson, we will cover the attributes of TRAC and
- how to program it in its own machine language.
-
- TRAC has the same general hardware configuration as other computers.
- Some of the specifics are shown in Figure I-2.
-
- +----------------------+
- | CONTROL |
- +<<<<<| (P register is 4 |>>>>>>>>>>>>>+
- v | decimal digits, I | v
- v +<| register is 7 digits |>>>>>>>>>+ v
- v v +----------------------+ v v
- v v v ^ v_ v
- v v v | / | v
- v_ v v | / | v
- / | v +----------------------+ __/ | v
- / | v | MEMORY |-->|Console| v
- __/ | v | 2000 decimal words | +-------+ v
- |Console|-->| Each word is 9 | +-------+ v
- +-------+ v | digits plus sign |-->|Printer|<+
- v +----------------------+ +-------+
- v | ^
- v | |
- v v |
- v +----------------------+
- +>| ARITHMETIC UNIT |
- |(Accumulator is 9 |
- | digits plus sign)|
- +----------------------+
-
- Figure I-2
-
-
-
- I-2
-
-
- The specifics for each of the five categories of elements is as
- follows:
-
- a. MEMORY - The memory consists of 2000 words whose ad-
- dresses range from 0000 to 1999. Any other addresses, if
- used, will cause unpredictable results (including the
- possibility of "locking up" the PC). Each word is 9
- decimal digits (plus sign) in length. A word may contain
- either a numeric value or an instruction.
-
- b. CONTROL - The control unit calls instructions from
- memory, decodes them, and controls all other elements to
- perform the required actions. The control unit contains a
- 4 decimal digit register called the P register which
- contains the memory address of the current instruction.
- The value in the P register is called the "location" of
- the instruction. Normally after each instruction is
- executed, the P register automatically increments by one.
- Therefore, consecutive instructions usually reside in
- consecutive memory locations. (In the discussion of the
- instruction repertoire, means for altering the sequence of
- instructions will be discussed.) The control unit also
- contains a 7 decimal digit register called the I register
- which contains the current instruction. The I register
- consists of three parts as illustrated in Figure I-3.
-
- +-+-+-+-+-+-+-+
- | | | | | | | |
- +-+-+-+-+-+-+-+
- ^ ^ ^ ^ ^
- ^ ^-^ ^-----^
- ^ ^ ^
- | | |
- T O A
- a p d
- g d
- c r
- o e
- d s
- e s
-
- Figure I-3
-
- The first digit of the I register is called the "TAG"
- field and may contain any single decimal digit. Its use
- will be discussed under the subject "Index registers".
- For most instructions, this field will be blank or zero.
-
- The next two digits contain the operation code. This
- specifies the operation to be performed. Only certain
- operations are available. Each available operation will
- be discussed in the section on operations. Each operation
- requires an operand. That is, something to be operated
- on, to, or with.
-
-
-
- I-3
-
-
- The last four digits of the I register contains the memory
- address of the operand. The operand field is also re-
- ferred to as the address field. It is usually the address
- of a memory location that contains a value to be operated
- on. In shift operations, this field gives the amount to
- shift rather than a memory address. In branch operations,
- this field gives an alternative memory location for chang-
- ing the sequence of instructions being performed. The
- address field will be explained more fully in the discus-
- sions of operations and index registers.
-
- c. ARITHMETIC UNIT - This unit consists of one 9 decimal
- digit (plus sign) register called the A register (or
- accumulator). It can perform the elementary operations of
- addition, subtraction, multiplication, and division.
- Other data manipulations can also be performed in the A
- register. These and the arithmetic operations will be
- discussed in the section on operations.
-
- d. INPUT - TRAC has only one input device. This device is
- the console keyboard and only numeric data may be entered.
- The specifics of the input data format will be discussed
- in the section on operations.
-
- e. OUTPUTS - TRAC has two output devices. One device is
- the console screen and the other is the printer.
-
- OPERATIONS
-
- For ALL instructions, the control unit fetches the contents of the
- memory location specified by the address field of the I register.
- On some operations, this value is needed but on others it is not
- needed. Since the given address (see note) is always accessed, it
- must always be a valid address.
-
- (Note: The address may be an effective address, see discussion of
- index registers.)
-
- The actions for each operation code is given in the following. For
- each operation, a two digit numeric operation code is given; and
- also, a two character mnemonic operation code is given in parenthe-
- ses. This is not required for machine language coding and can be
- ignored for the present. It will be referred to in Lessons 3 and 4.
-
- 00 (LD) LoaD the A register. This operation causes the operand
- (contents of the location specified by the address) to
- replace the contents of the A register.
-
- 01 (AD) ADd to the A register. Causes the operand to be added to
- the A register. The sum replaces the previous contents of
- the A register. (The programmer must use care to avoid
- causing an overflow. TRAC will permit an overflow but it
- will save only the least significant 9 digits and you will
- NOT get an error message.)
-
-
-
- I-4
-
-
- 02 (SU) SUbtract from the A register. Causes the operand to be
- subtracted from the A register. The difference replaces the
- previous contents of the A register. (The warning on over-
- flows applies to subtraction as well as addition.)
-
- 03 (ST) STore the contents of the A register. The contents of the A
- register is stored at the location specified by the address.
- The contents of the A register remain unchanged.
-
- 15 (MU) MUltiply the A register by the operand. The operand is the
- multiplier and the contents of the A register is the multi-
- plicand. The resulting product replaces the previous con-
- tents of the A register. Since the contents of the A regis-
- ter can not exceed 9 decimal digits, the sum of the digits
- in the multiplier and the multiplicand must not exceed 9.
- (e.g. If the multiplier is a 4 digit number, then the multi-
- plicand may not have more than 5 digits.) If this condition
- is not satisfied, an overflow will occur and no error mes-
- sage is given. An example program will be given later to
- demonstrate how to obtain a full 18 digit product from two 9
- digit factors.
-
- 16 (DV) DiVide the A register by the operand. The operand is the
- divisor and the A register is the dividend. The quotient
- replaces the previous contents of the A register. Since
- this operation is division of one integer by another giving
- only the integral part of the quotient (no rounding), there
- can be no overflow as long as the divisor is non-zero.
-
- 24 (BN) Branch on Negative. The operand is the address itself. If
- the contents of the A register is negative, branch to the
- location specified by the operand to obtain the next in-
- struction. If the A register is zero or positive, obtain
- the next instruction from the next consecutive location.
-
- 25 (BZ) Branch on Zero. The operand is the address itself. If the
- contents of the A register is zero, branch to the location
- specified by the operand to obtain the next instruction. If
- the A register is non-zero, obtain the next instruction from
- the next consecutive location.
-
- 26 (BU) Branch Unconditionally. The operand is the address itself.
- Branch to the location specified by the operand to obtain
- the next instruction.
-
- 36 (SR) Shift Right. The operand is the value of the effective
- address. Shift the contents of the A register right the
- number of decimal places specified by the address. Digits
- shifted out of the right end of the A register are lost.
- The positions vacated at the left end of the A register are
- filled with zeroes. The sign of the A register is pre-
- served. Although right shifts of more than 9 places are
- permissible, they have no utility.
-
-
-
-
- I-5
-
-
- 37 (SL) Shift Left. The operand is the value of the effective
- address. Shift the contents of the A register left the
- number of decimal places specified by the address. Digits
- shifted out of the left end of the A register are lost. The
- positions vacated at the right end of the A register are
- filled with zeroes.
-
- 40 (RD) ReaD input data. This operation allows five values to be
- read from the console keyboard into five consecutive memory
- locations. The first value is read into the location speci-
- fied by the address field; the next four are read into the
- next four consecutive locations. The address must not
- exceed 1995. When the program encounters the read command,
- the console will display a '?' followed by a space and will
- wait for the manual entry of up to five values. The format
- for entering data values is almost free-form. Values are
- entered as whole numbers (preceded by a '-', if negative)
- and separated from each other by one or more spaces. Each
- value must not exceed 11 digits, including sign and spaces.
- The entering of values is terminated by pressing the "ENTER"
- key. You must enter at least one value, even if zero.
-
- 41 (PC) Print to Console. This operation causes up to five values
- to be displayed on the console screen. The five values are
- output from five consecutive memory locations beginning with
- the memory location specified by the effective address. The
- effective address cannot exceed 1995.
-
- 42 (PP) Print to printer. This operation is identical to the previ-
- ous print command but the output is sent to the printer.
-
- 50 (HT) HalT. This operation stops the execution of the program.
- ALL programs should be terminated with a halt command. The
- address has no meaning but it still must be a valid address.
-
- (Note: Any operation other than those given will give an error
- message, set the op code to 99, and continue.)
-
- INDEX REGISTERS
-
- TRAC has nine index registers. These registers are numbered 1
- through 9 and are actually memory locations 0001 through 0009. If
- you use any of these as index registers, you normally would avoid
- their use for instructions or constants. The purpose of an index
- register is to provide a simple method of modifying an instruction.
- An index register is used by placing its number in the TAG field of
- the instruction to be modified. When the TAG field is non-zero, the
- contents of the specified index register is added to the address of
- the instruction to give a new address called the effective address.
- The effective address must always be a valid machine address.
-
- Thus, if index register 2 contains the value, 3, the instruction:
-
- +-+-+-+-+-+-+-+
- |2|0|1|1|0|0|4|
- +-+-+-+-+-+-+-+
-
- I-6
-
-
- would cause the contents of the memory location 1007 (1004 plus 3)
- to be added to the A register. Or, in the case of the following
- shift operation:
-
- +-+-+-+-+-+-+-+
- |2|3|6|0|0|0|1|
- +-+-+-+-+-+-+-+
-
- the contents of the A register would be shifted right 4 (1 plus 3)
- places.
-
- EXAMPLE
-
- Convert an octal number (base 8) to decimal. The flow chart of
- Figure I-4 provides a method for converting an octal number to
- decimal. For those not familiar with numbers in bases other than
- 10, the following explanation is given. Suppose the number, 637 is
- given and it is identified as being in base 8. The position of a
- digit in the number specifies that that digit is to be multiplied by
- a power of the base (in this case 8) in order to give its actual
- value. In the example, the number represents 6 * 64 + 3 * 8 + 7.
- In decimal this would calculate to be 415. Note that no digit of
- the number can be as great as the base. Therefore, to convert a
- number from base 8, we must isolate each digit of the number, multi-
- ply it by the appropriate power of 8 and then sum the individual
- products.
-
- In the method illustrated,
-
- we first enter the octal number that is to be converted.
-
- Then we test to see if it is zero.
-
- If it is we stop the program.
-
- Next we determine if the number is positive or negative.
-
- If it is negative, we must temporarily make it positive for the
- conversion. Later we will change it back.
-
- Next we set an index register to 1 (this is for counting places)
-
- then we isolate the first (high order) digit and save it where the
- answer is developed.
-
- We then start a loop in which we multiply the partial answer by 8,
- isolate a digit, and add it to the partial result.
-
- The index is incremented and if it is less than 9, the loop is
- repeated.
-
- Otherwise, the sign is restored to the input value and the result
- and the answer are displayed.
-
-
-
-
- I-7
-
-
- |
- v
- ----------
- +----------->/ Read X /
- | ---------
- | |
- | v
- | / \ Equal +------+
- | <X:0>------>| HALT |
- | \./ +------+
- | | Not equal
- | v
- | +--------+ > / \ < +--------+
- | | S = +1 |<---<X:0>--->| S = -1 |
- | +--------+ \./ | X = -X |
- | | +--------+
- | v v
- | +------------------------+
- | | I = 1 |
- | | Shift X right 8 places |
- | | Store result at Y |
- | +------------------------+
- | |
- | v
- | +------------------------+
- | | Y = Y * 8 |
- | +->| Shift X left I places |
- | | | Shift right 8 places |
- | | | Add to Y |
- | | +------------------------+
- | | |
- | | v
- | | +-----------+
- | | | I = I + 1 |
- | | +-----------+
- | | |
- | | v
- | | < / \
- | +------------<I:9>
- | \./
- | | >=
- | v
- | +-----------+
- | | X = S * X |
- | | Y = S * Y |
- | +-----------+
- | |
- | v
- | --------------
- +----------/ Display X,Y /
- --------------
-
- Figure I-4
-
-
-
-
- I-8
-
-
- The code in Figure I-5 gives one way that the flow chart could be
- coded in TRAC. Some variations of this code are possible and may
- even be better. The example uses all the operations except divide
- and print to printer.
-
- Here are some additional features of the code that should be noted.
- The last line of a program must contain 9999 in the location field
- and the address of the first instruction of the program must appear
- in the address field.
-
- Columns 1 through 4 are to contain the location of an instruction
- (or constant).
-
- Column 5 must be blank.
-
- Columns 6 through 15 are used for entering a constant or an instruc-
- tion. These columns are used differently for constants than they
- are for instructions. Constants may be placed anyplace within these
- columns (no embedded blanks). Instructions must also leave columns
- 6, 7, and 8 blank. The tag field is column 9, columns 10 and 11 are
- for the operation code, and columns 12 through 15 are used for the
- operand field.
-
- Column 16 should be blank.
-
- Columns 17 through 80 may be used for comments. You are encouraged
- to include comments since it is very difficult to understand machine
- code without comments. These comments are ignored by TRAC.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- I-9
-
-
-
- L O A
- O P D
- C E D
- R R
- 0010 401000 Put X into 1000 (Sets 1001 - 1004 to zero)
- 0011 001000 Load accumulator with X
- 0012 250048 If X is zero jump to halt instruction
- 0013 240017 If X is negative, skip next 3 instructions
- 0014 001501 Load +1
- 0015 031502 Store at S (sign)
- 0016 260022 Branch unconditionally around next 5
- 0017 001503 Load -1
- 0018 031502 Store at S
- 0019 001500 Load zero
- 0020 021000 Subtract X (This makes a negative X positive)
- 0021 031000 Store back at X
- 0022 001501 Load 1
- 0023 030002 I=1 using index register 2
- 0024 001000 Load X
- 0025 360008 Shift it right 8
- 0026 031001 Store at Y
- 0027 001001 Load Y (Note this is the start of a loop)
- 0028 151504 Multiply by 8
- 0029 031001 Store at Y
- 0030 001000 Load X
- 0031 2370000 Shift left by I (Contents of register 2)
- 0032 360008 Shift right 8 (This isolates an octal digit)
- 0033 011001 Add previous Y
- 0034 031001 Store back at Y
- 0035 000002 Load I
- 0036 011501 Add 1
- 0037 030002 Store at I
- 0038 021505 Subtract 9 to see if done
- 0039 240027 If negative repeat loop
- 0040 001000 Load X
- 0041 151502 Multiply by sign
- 0042 031000 Store at X
- 0043 001001 Load Y
- 0044 151502 Multiply by sign
- 0045 031001 Store at Y
- 0046 411000 Print X, Y
- 0047 260010 Branch back to start
- 0048 500000 Halt
- 1500 0 Zero
- 1501 1 One
- 1503 -1 Negative one
- 1504 8 Eight
- 1505 9 Nine
- 9999 10 End of code and starting location
-
- Figure I-5
-
-
-
-
-
- I-10
-
-
- The following flow chart is exercise 1. In this exercise, you are
- to read five values from your keyboard, sum them using an index
- register in a loop and then display the sum. The program will keep
- prompting you for values until you enter a zero in the first posi-
- tion. Code this problem now and enter it into a file. After com-
- pleting lesson 2 you should try running the program to see if it
- functions correctly.
-
- |
- v
- --------------------------------
- +-->/ Read X(0),X(1),X(2),X(3),X(4) /
- | --------------------------------
- | |
- | v
- | / \
- | / \ = +------+
- | <X(0):>--->| STOP |
- | \ 0/ +------+
- | \./
- | | Not =
- | v
- | +-------+
- | | I = 0 |
- | | S = 0 |
- | +-------+
- | |
- | v
- | +---------------+
- | | S = S + A(I) |<--+
- | +---------------+ |
- | | |
- | v |
- | +-----------+ |
- | | I = I + 1 | |
- | +-----------+ |
- | | |
- | v |
- | / \ < |
- | <I:5>---------+
- | \./
- | | >=
- | v
- | ----------
- +--------------/ Print S /
- ----------
-
- Exercise I-1
-
- Note: When an expression such as I:4 appears inside a diamond, this
- means that I and 4 are to be compared (usually by finding I-4) and
- then testing and branching according to the conditions shown on the
- flow chart.
-
-
-
-
- I-11